package com.hiwhitley.chapter05;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Created by hiwhitley on 17-1-14.
 */
public class IsUniquel {
    public boolean isUniquel(char[] chars) {
        if (chars == null) {
            return true;
        }
        boolean[] map = new boolean[256];
        for (int i = 0; i < chars.length; i++) {
            if (map[chars[i]]) {
                return false;
            }
            map[chars[i]] = true;
        }
        return true;
    }

    public boolean isUniquel2(char[] chars) {

        if (chars == null) {
            return true;
        }

        HashSet<Character> map = new HashSet<>();

        for (int i = 0; i < chars.length; i++) {
            if (map.contains(chars[i]))
                return false;
            map.add(chars[i]);
        }
        return true;
    }

    public static void main(String[] args) {
        IsUniquel main = new IsUniquel();
        System.out.println(main.isUniquel2(new char[]{'a', 'b', 'c'}));
        System.out.println(main.isUniquel2(new char[]{'1', '2', '1'}));


    }
}
